<?php
/** 
 * Created by RisingSun. 
 * User: MarkSpace 
 * Date: 20210822
 * Time: 11:05 
 * 矿机管理
 */
namespace app\product\model;

use app\product\model\Model;
use Mscore\Core\Lib\Upload;

class Product extends Model{
	
    private $category = 'pro_category';

    /**
     * 類別列表
     * @param array $catelist
     * @param array $where
     */ 
    public function catelist($where=null){
        //類別列表
        $catelist = Model::name($this->category)
            ->where($where)
            ->order('id asc')
            ->select();
        return $catelist; 
    }

    /**
     * 類別信息
     * @param array $cateinfo
     * @param array $where
     */ 
    public function cateinfo($id=null,$field='*'){
        $info = Model::name($this->category)
          ->field($field)
          ->where(['id'=>$id])
          ->find();
        if(isset($info['titles'])&&!empty($info['titles'])){
            $info['titles']=json_decode($info['titles'],true);
            $i=0;
            foreach ($info['titles'] as $key => $value) {
                 $info['langinfo'][$key]['titles']=$value;
                 $info['langinfo'][$key]['id']=$i;
                 $i++;
             }  
        }
        return $info;
    }

    /**
     * 類別保存
     * @param array $catesave
     * @param array $where
    */ 
    public function catesave($data){
        //获取封面图片
        $cateimg=[];
        if(isset($data['id'])&&!empty($data['id'])){
            $cateimg = $this->cateinfo($data['id'],'imgurl');
        }
        if(isset($data['langs'])&&!empty($data['langs'])){
            foreach ($data['langs'] as $key => $value) {
                $titles[$value]=$data['titles'][$key];
            }
        };
        switch (true) {
            case isset($data['imgOne'])&&!empty($data['imgOne']):
                $url="/static/upload/product/";
                $imgurl = (new Upload)->Uploadbase64($data['imgOne'],$url);
                if(isset($cateimg['imgurl'])&&file_exists(config('file.path') . $cateimg['imgurl'])){
                    @unlink (config('file.path') . $cateimg['imgurl']); 
                }
                break;
            default:
                $imgurl=isset($cateimg['imgurl'])?$cateimg['imgurl']:'';
                break;
        }

        $data  = [
            'id'=>$data['id'],
            'title'=>$data['title'],
            'titles'=>json_encode($titles,JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE),
            'acode'=>$data['acode'],
            'pagecode'=>!empty($data['pagecode'])?$data['pagecode']:$data['acode'],
            'pid'=>$data['pid'],
            'imgurl'=>$imgurl,
            'notes'=>$data['notes'],
            'status'=>$data['status'],
            'is_index'=>$data['is_index'],
            'orderid'=>$data['orderid']
        ];
        switch (true) {
            case isset($data['id'])&&!empty($data['id']):
                $id=$data['id']; unset($data['id']);
                Model::name($this->category)->where('id=' . $id)->update($data);
                return true;
                break;
            
            default:
                unset($data['id']);
                Model::name($this->category)->insert($data);
                return true;
                break;
        }
        return false;
    }

     /**
     * 類別删除
     * @param array $catedel
     * @param array $where
     */
    public function catedel($id){
         //删除
        if ($id) {
            Model::name($this->category)->where('id='.$id)->delete();
            return true;
        }
        return false;
    }

}
